<?php

namespace App\models;

/**
*
*/
class Disk extends \App\base\Model
{
  const _disk = 'disk_info';
  const _directory = 'disk_directory';
  const _file = 'disk_file';

  public function getInfo($token)
  {
    return $this->_db->get(self::_disk,[
      "[>]user_auth" => "uid",
    ],[
      'disk','quota'
    ],[
      'access_token' => $token
    ]);
  }

  public function getUsed($disk)
  {
    return $this->_db->sum(self::_file,[
      'file_size'
    ],['disk' => $disk]);
  }

  public function hasFile($directory,$name)
  {
    return $this->_db->has(self::_file,[
      'directory' => $directory,
      'tag' => $name
    ]);
  }

  public function hasDir($directory)
  {
    return $this->_db->has(self::_directory,[
      'directory' => $directory
    ]);
  }

  public function mkdir($disk,$parent,$name)
  {
    if ($this->_db->has(self::_directory,[
        'disk' => $disk,
        'parent' => $parent,
        'tag' => $name
      ])) {
        return false;
    }
    $this->_db->insert(self::_directory,[
      'disk' => $disk,
      'parent' => $parent,
      'tag' => $name
    ]);
    return $this->_db->has(self::_directory,[
        'disk' => $disk,
        'parent' => $parent,
        'tag' => $name
      ])?true:false;
  }

  public function getRootDir($disk)
  {
    return $this->_db->get(self::_directory,[
      'directory','tag'
    ],[
      'parent' => 0
    ]);
  }

  public function getNode($directory,$disk)
  {
    $result['directory'] = $directory;
    $result['tag'] = $this->_db->get(self::_directory,['tag'],['directory' => $directory])['tag'];
    $result['files'] = $this->_db->select(self::_file,[
      'file','directory','tag','ext','file_size','create_at','update_at'
    ],['directory' => $directory,'disk' => $disk,'status' => 1]);
    $result['dirs'] = $this->_db->select(self::_directory,[
      'directory','tag','create_at','update_at'
    ],['parent' => $directory,'disk' => $disk,'status' => 1]);
    return $result;
  }

  public function getDirs($disk)
  {
    return $this->_db->select(self::_directory,[
      'directory','parent','tag'
    ],['disk' => $disk,'status' => 1]);
  }

  public function remove($dirs,$files)
  {
    $this->_db->delete(self::_directory,[
      'directory' => $dirs
    ]);
    $this->_db->delete(self::_file,[
      'file' => $files
    ]);
  }

  public function upload($disk,$directory,$tag,$ext,$file_size,$fdfs)
  {
    $this->_db->insert(self::_file,[
      'disk' => $disk,
      'directory' => $directory,
      'tag' => $tag,
      'ext' => $ext,
      'hash' => '',
      'file_size' => $file_size,
      'fdfs' => $fdfs,
      'status' => 1
    ]);
  }

  public function move($dirs,$files,$to)
  {
      $this->_db->update(self::_directory,[
        'parent' => $to,
      ],[
        'directory' => $dirs
      ]);
      $this->_db->update(self::_file,[
        'directory' => $to
      ],[
        'file' => $files
      ]);
  }

  public function getFDFS($file)
  {
    return $this->_db->get(self::_file,[
      'fdfs','ext'
    ],[
      'file'=>$file
    ]);
  }
}
